package software.literature.model.bean;

import java.lang.reflect.Field;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;

import software.literature.view.type.DataUnitView;

public abstract class BBGData {
	protected int id = -1;
	protected String noteText = "";
	protected String noteUrl = "";
	protected int treeId = -1;
	protected int type;
	protected String url;
	protected String[]  fieldsNames;
	public void setUrl(String url) {
		if (url != null) {
			this.url = url;
		} else {
			url = "";
		}
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getNoteText() {
		return noteText;
	}

	public void setNoteText(String noteText) {
		if(null == noteText){
			this.noteText = "";
		}else{
			this.noteText = noteText;	
		}
	}

	public String getNoteUrl() {
		return noteUrl;
	}

	public void setNoteUrl(String noteUrl) {
		if(null == noteUrl){
			this.noteUrl = "";
		}else{
			this.noteUrl = noteUrl;	
		}
	}

	public int getTreeId() {
		return treeId;
	}

	public void setTreeId(int treeId) {
		this.treeId = treeId;
	}
	/**
	 * get the four fields that will be shown on the table view,so the return
	 * size is 4.
	 * 
	 * @return
	 */
	public abstract String[] getTableFormat();

	public abstract void parse(String bbgRecords);

	public String getUrl() {
		// TODO Auto-generated method stub
		return url;
	}

	public String getData() {
		// TODO Auto-generated method stub
		StringBuilder build = new StringBuilder();
		try {
			Field[] field = this.getClass().getDeclaredFields();
			for (int i = 0; i < field.length; i++) {
				String str=(String) field[i].get(this);
				if(str==null||str.trim().equals("")){
					build.append(" ");	
				}else{
					build.append(str.trim());	
				}
				build.append("##");
			}
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return build.toString();
	}
	/**
	 * check whether the filed 'filed' contains value 'value'
	 * @param field
	 * @param value
	 * @return
	 */
	public abstract boolean contain(String field,String value);
	/**
	 * return the field index according to the field name
	 * @param field
	 * @return
	 */
	public int getValIndex(String field){
	   	 for(int i=0;i<fieldsNames.length;i++){
	   		 if(fieldsNames[i].equals(field)){
	   			 return i;
	   		 }
	   	 }
	   	 return -1;
	    }

	public abstract String getRTFFormat();
/**
 * check whether this object's all fields contains any of the value in fieldValues
 * @param fieldValues
 * @return
 */
	public boolean contain(String[] fieldValues) {
		// TODO Auto-generated method stub
		for(int i=0;i<fieldValues.length;i++){
			if(contain(fieldValues[i])){
				return true;
			}
		}
		return false;
	}
/**
 * check whether this object's all fileds value contain value 'string'
 * @param string
 * @return
 */
	public abstract boolean contain(String string) ;
}
